﻿function Fixed() {

    //固定
    var ie6 = /msie 6/i.test(navigator.userAgent), dv = $('#right_container'), st;
    var left = dv.offset().left;
    dv.attr('otop', dv.offset().top);

    st1 = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
    if (st1 >= parseInt(dv.attr('otop'))) {
        if (ie6) {//IE6不支持fixed属性，所以只能靠设置position为absolute和top实现此效果
            dv.css({ position: 'absolute', top: st1 });
        }
        else if (dv.css('position') != 'fixed')
        //dv.attr("class","");
            dv.css({ 'position': 'fixed', top: 0, 'width': 970 });
    } else if (dv.css('position') != 'static')
        dv.css({ 'position': 'static', 'width': 970 });

    $(window).scroll(function () {

        //        if ($("#left_container").height() <= $("#right_container").height()) {
        //                    return;
        //                }

        st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
        if ($("#left_container").height() > $("#right_container").height() && st >= parseInt(dv.attr('otop'))) {
            if (ie6) {//IE6不支持fixed属性，所以只能靠设置position为absolute和top实现此效果
                dv.css({ position: 'absolute', top: st });
            }
            else if (dv.css('position') != 'fixed')
            //dv.attr("class","");
                dv.css({ 'position': 'fixed', top: 0, 'width': 970 });
        } else if (dv.css('position') != 'static')
            dv.css({ 'position': 'static', 'width': 970 });

        st2 = 0 - Math.max(document.body.scrollLeft || document.documentElement.scrollLeft);
        //alert(st2);
        if (st2 != 0) {
            if (dv.css('position') == 'fixed') {
                dv.css({ left: st2 });
            }
        }

    });
    $(window).resize(function () {

        //        if ($("#left_container").height() <= $("#right_container").height()) {
        //                    return;
        //                }

        st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
        if ($("#left_container").height() > $("#right_container").height() && st >= parseInt(dv.attr('otop'))) {
            if (ie6) {//IE6不支持fixed属性，所以只能靠设置position为absolute和top实现此效果
                dv.css({ position: 'absolute', top: st });
            }
            else if (dv.css('position') != 'fixed')
            //dv.attr("class","");
                dv.css({ 'position': 'fixed', top: 0, 'width': 970 });
        } else if (dv.css('position') != 'static')
            dv.css({ 'position': 'static', 'width': 970 });

        st2 = 0 - Math.max(document.body.scrollLeft || document.documentElement.scrollLeft);
        if (st2 != 0) {
            if (dv.css('position') == 'fixed') {
                dv.css({ left: st2 });
            }
        }
    });
}